package com.cn.ycy.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * 使用原生quartz实现
 */
public class SchedulerTest {


    public static void main(String[] args) throws SchedulerException {

        //创建一个scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("skey", "svalue");

        //创建一个Trigger
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("mygroup", "group1")
                .usingJobData("TriggerKey1", "ex")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3)
                        .repeatForever()).build();
        trigger.getJobDataMap().put("TriggerKey2", "TriggerData2");


        //创建一个job
        JobDetail job = JobBuilder.newJob(HelloJob1.class)
                .usingJobData("j1", "jv1")
                .withIdentity("myjob", "group1").build();
        job.getJobDataMap().put("j2", "jv2");


        //注册trigger并启动scheduler
        scheduler.scheduleJob(job, trigger);

        //创建一个job
        JobDetail job2 = JobBuilder.newJob(HelloJob2.class)
                .usingJobData("j1", "jv1")
                .withIdentity("myjob2", "group1").build();
        job.getJobDataMap().put("j2", "jv2");

        // 2. 定义一个Trigger，定义该job立即执行,并且每秒都执行一次。重复无数次
        CronTrigger cronTrigger = (CronTrigger) TriggerBuilder.newTrigger()
                .withIdentity("cronTrigger", "group1")// 定义名字和组
                .withSchedule(    //定义任务调度的时间间隔和次数
                        CronScheduleBuilder
                                .cronSchedule("0/10 * * * * ? *")
                )
                .build();
        cronTrigger.getJobDataMap().put("cronTriggerKey", "cronTriggerData");

        scheduler.scheduleJob(job2, cronTrigger);

        //TODO 这个是做什么的？
//    TriggerKey triggerKey = TriggerKey.triggerKey("trigger1", "group1");

//    scheduler.pauseTrigger(triggerKey);

        scheduler.start();


    }


}
